package cn.biplam.setup.code.product;

import java.util.ArrayList;

import cn.biplam.back.sql.dml.Delete;
import cn.biplam.back.sql.dml.Insert;
import cn.biplam.back.sql.dml.Update;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Sequence;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.setup.code.Data;
import cn.biplam.setup.code.ajx.AjxCodeData;
import cn.biplam.setup.code.bean.BeanCodeData;
import cn.biplam.setup.code.opera.OperatorCodeData;
import cn.biplam.setup.code.page.PageCodeData;

/**
 * 产品管理数据操作
 * @author XiaoRuxing Sep 29, 20099:17:07 AM
 * @version 1.0
 */
public class ProductData extends Data{

	private String tableName = "Sys_Product";
	
	public static String PRODUCT_ID = "p_id";
	public static String PRODUCT_NAME = "p_name";
	public static String PRODUCT_DESC = "p_desc";
	public static String SERVICE_FLG = "p_service";
	
	/**
	 * 获取所有产品
	 * @return SqlResult
	 */
	public SqlResult getAll(){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		return this.executeSelectSql(sp.toString());
	}
	
	/**
	 * 获取指定产品
	 * @param productId 产品ID
	 * @return SqlResult
	 */
	public SqlResult get(String productId){
		StringBuilder sp = new StringBuilder();
		sp.append(" select * from ");
		sp.append(this.tableName);
		sp.append(" where ");
		sp.append(PRODUCT_ID);
		sp.append("='");
		sp.append(productId);
		sp.append("'");
		return this.executeSelectSql(sp.toString());
	}
	
	/**
	 * 添加产品
	 * @param filter  产品信息
	 * @param mUser   用户信息
	 * @throws ErrorMessage
	 */
	public void add(DataFilter filter,UserInfo mUser) throws ErrorMessage{
		Insert insert = new Insert(this.tableName);
		String productId = Sequence.getSequence(mUser.getUserID());
		insert.putStringFieldDenyNull(PRODUCT_ID, productId);
		insert.putStringFieldIgnoreNull(PRODUCT_NAME, 
				filter.getValue(PRODUCT_NAME));
		insert.putStringFieldIgnoreNull(PRODUCT_DESC,
				filter.getValue(PRODUCT_DESC));
		insert.putStringFieldDenyNull(SERVICE_FLG, 
				filter.getValue(SERVICE_FLG));
		this.executeDMLSql(insert.getSimpleSql());
	}
	
	/**
	 * 编辑产品信息
	 */
	public void update(DataFilter filter) throws ErrorMessage{
		String productId = filter.getValue(PRODUCT_ID);
		String serviceFlg = filter.getValue(SERVICE_FLG);
		Update update = new Update(this.tableName);
		update.putStringFieldIgnoreNull(PRODUCT_NAME, 
				filter.getValue(PRODUCT_NAME));
		update.putStringFieldIgnoreNull(PRODUCT_DESC,
				filter.getValue(PRODUCT_DESC));
		update.putStringFieldIgnoreNull(SERVICE_FLG, 
				serviceFlg);
		StringBuilder sp = new StringBuilder();
		sp.append(PRODUCT_ID);
		sp.append("='");
		sp.append(productId);
		sp.append("'");
		update.setWhere(sp.toString());
		
		boolean statChange = this.getStatuChange(productId, serviceFlg);
		ArrayList<String> sql = new ArrayList<String>();
		sql.add(update.getSimpleSql());
		if(statChange){
			this.productModelStatus(sql, serviceFlg, productId);
		}
		TOOLS.printList(sql);
		this.executeDMLSqlBatch(TOOLS.listToStringArray(sql));
	}
	
	/**
	 * 获取产品状态是否改变
	 */
	private boolean getStatuChange(String productId,String statues) throws ErrorMessage{
		boolean statChang = false;
		if(statues==null){
			statChang = false;
		}else{
			String oldStaues = this.getStatus(productId);
			if(statues.equals(oldStaues)){
				statChang = false;
			}else{
				statChang = true;
			}
		}
		return statChang;
	}
	
	/**
	 * 设置模块状态
	 */
	private ArrayList<String> productModelStatus(ArrayList<String> statusUpdate,
			String status,String productId) throws ErrorMessage{
		statusUpdate.add(new AjxCodeData().updateStatus(status, productId));
		statusUpdate.add(new OperatorCodeData().updateStatus(status, productId));
		statusUpdate.add(new BeanCodeData().updateStatus(status, productId));
		statusUpdate.add(new PageCodeData().updateStatus(status, productId));
		return statusUpdate;
	}
	
	/**
	 * 获取产品下是否存在模块
	 * @param productId
	 * @return boolean
	 */
	public boolean getProductHaveModel(String productId){
		SqlResult sr = new AjxCodeData().getProductCodes(productId);
		if(TOOLS.exist(sr)){
			return true;
		}
		sr = new OperatorCodeData().getProductCodes(productId);
		if(TOOLS.exist(sr)){
			return true;
		}
		sr = new BeanCodeData().getProductCodes(productId);
		if(TOOLS.exist(sr)){
			return true;
		}
		sr = new PageCodeData().getProductCodes(productId);
		if(TOOLS.exist(sr)){
			return true;
		}
		return false;
	}
	
	/**
	 * 删除产品
	 */
	public void del(String productId) throws ErrorMessage{
		Delete delete = new Delete(this.tableName);
		StringBuilder sp = new StringBuilder();
		sp.append(PRODUCT_ID);
		sp.append("='");
		sp.append(productId);
		sp.append("'");
		delete.setWhere(sp.toString());
		this.executeDMLSql(delete.getSimpleSql());
	}
	
	/**
	 * 获取制定产品状态
	 * @param productId 产品ID
	 * @return String
	 * @throws ErrorMessage
	 */
	public String getStatus(String productId) throws ErrorMessage{
		SqlResult product = this.get(productId);
		String status = null;
		if(TOOLS.exist(product)){
			product.next();
			status = product.getString(SERVICE_FLG);
		}
		return status;
	}
	
	/**
	 * 产品服务状态
	 */
	public static class ProductStaus{
		/** 停止 */
		public static final String Stop =  "0";
		/** 发布 */
		public static final String Start = "1";
	}
}
